home *** CD-ROM | disk | FTP | other *** search
- MS-C compiler note 1
- 9/23/85 by Tom Thompson
-
- The C compiler has some problems converting near code pointers to far code
- pointers. The extract from a .COD file below illustrates the problem.
-
- void f(void); /* f - fwd def of near func */
- void (*fp)(void); /* fp - near pointer to func */
- void (far *fpf)(void); /* fpf - far pointer to func */
-
- fpf = ( void (far *)(void) ) f;
- mov ax,OFFSET _f
- mov WORD PTR _fpf,ax
- mov WORD PTR _fpf+2,cs ;uses CS as desired
- fpf = ( void (far *)(void) ) fp;
- mov ax,_fp
- mov WORD PTR _fpf,ax
- mov WORD PTR _fpf+2,ds ;uses DS, not desired
-
- The first statement works as desired. It sets fpf's ofs and seg to point
- to the function f. In the case where the right argument is a variable, it
- doesn't work as wanted -- it uses DS instead of CS. I can't decide whether
- this is a bug or just a limitation. Since this is a small model program, the
- compiler ought to know that all code refs are to current CS, however, if you
- assume any model possible, then you cannot construct a far pointer from a near
- with any certainy.
-
- I ran into this problem when passing a near func ptr to a function that
- subsequently converted it to a long pointer. eg:
-
- void fx(fp)
- void (*fp)(void);
- {
- void (far *fpf)(void);
- ...
- fpf = ( void (far *)(void) ) fp; /* generated wrong code */
- ...
- }
-
- The workaround, VALID ONLY FOR SMALL MODEL, is:
-
- void fx(fp)
- void (*fp)(void);
- {
- void (far *fpf)(void);
- ...
- fpf = fx; /* fix cs part of fpf */
- FP_OFS(fpf) = (unsigned) fp; /* put in desired offset */
- ...
- }